package com.fone.player.airone;

import java.net.InetAddress;
import java.net.UnknownHostException;

import android.content.Context;
import android.os.RemoteException;

import com.fone.player.util.L;

import dlna.CDLNAPlayer;

public class FDLNAPlay {

	public static final int RS_OK = 0;
	public static final int RS_ERROR = -1;

	private static CDLNAPlayer dlna_player;
	/**
	 * get dlna player
	 * @param context
	 * @return CDLNAPlayer
	 * @throws
	 */
	public static CDLNAPlayer getDLNA(Context context){
		if( dlna_player == null )
			dlna_player = new CDLNAPlayer(context);
		return dlna_player;
	}
	/**
	 * stop dlna player
	 * @return void
	 * @throws
	 */
	public static void stop(){
		if( dlna_player != null )
			dlna_player.Stop();
		dlna_player = null;
	}
	
	/**
	 * play url 
	 * @param uuid device's server_uid
	 * @param file to play url
	 * @param title display title
	 * @param icon display icon url
	 * @param pos play postion
	 * @return int result
	 */
	public static int open(final String uuid, final String file,final String title,final String icon,final int pos  ) {
		new Thread(){
			public void run(){
				try {
					String url;
					String deviceManufactur = AirOne.getInstance().getService().getDeviceManufactur(uuid);
					L.i("airone","","airone dlna deviceManufactur:"+deviceManufactur);
					if ("100TV".equalsIgnoreCase(deviceManufactur)){
						url = file;
					} else {
						url = convertDnsToIpUrl(file);
					}
					L.i("airone","","airone dlna to open source:"+file);
					L.i("airone","","airone dlna to open url:"+url);
					int res = AirOne.getInstance().getService().dlna_open(uuid, url,title,icon,pos);
					last_state = NONE;
					if( res == FDLNAPlay.RS_OK ){
						FDLNARender.getInstance().dlna_on_open_success(uuid);
						return;
					}else
						L.i("airone","","airone dlna to open fail");
				} catch (RemoteException e) {
					L.i("airone","","airone dlna to open error:"+e);
				}
				FDLNARender.getInstance().dlna_on_open_error(uuid);
				//EventControler.fn_core_service_request(fn_Core.FN_AIRONE_BTN_OPEN_ERROR, fn_Core.FN_UI_EVT_TOUCH_UP,uuid,null);		
			}
		}.start();
		return FDLNAPlay.RS_OK;
		
	}
	/**
	 * convert domain name to ip ex:www.baidu.com->192.191.11.1
	 * @param file url
	 * @return 参数X描述... （注意：请将参数、描述都对齐）
	 * @return String
	 * @throws
	 */
	private static String convertDnsToIpUrl(String file){
		String url = null;
		String ip = null;
		try {
			int i_s = file.indexOf("://");
			if( i_s > -1 ){
				int i_e = file.indexOf('/',i_s+3);
				if( i_e > -1 ){
					String dns = file.substring(i_s+3,i_e);
					if( dns.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}.*")){
						url = file;
					}else{
						ip = InetAddress.getByName(dns).getHostAddress();
						url = file.replace(dns,ip);
					}
				}else
					url = file;
			}else
				url = file;
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
			url = file;
		}
		return url;
		
	}
	/**
	 * Deprecated, null implement method
	 * @param uuid
	 * @param start_per
	 * @return int
	 * @throws
	 */
	@Deprecated
	public static int start(String uuid, double start_per) {
		try {
			return AirOne.getInstance().getService().dlna_start(uuid,start_per);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return  FDLNAPlay.RS_ERROR;
	}

	/**
	 * pause dlna play video
	 * @param uuid device server_uid
	 * @return int
	 * @throws
	 */
	public static int pause(String uuid) {
		try {
			L.i("airone","","java pasuse");
			return AirOne.getInstance().getService().dlna_pause(uuid);
		} catch (RemoteException e) {
			e.printStackTrace();
			L.i("airone","","pause"+e.toString());
		}
		return  FDLNAPlay.RS_ERROR;
	}

	/**
	 * resume play dlna video
	 * @param uuid device server_uid
	 * @return int result
	 * @throws
	 */
	public static int resume(String uuid) {
		try {
			return AirOne.getInstance().getService().dlna_resume(uuid);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return  FDLNAPlay.RS_ERROR;
	}
	/**
	 * seek to point to play dlna video
	 * @param uuid device server_uid
	 * @param time point
	 * @return int result
	 * @throws
	 */
	public static int seek(final String uuid,final int time) {
		
		new Thread(){
			public void run(){
				
				try {
					AirOne.getInstance().getService().dlna_seek(uuid,time);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		}.start();
		return  FDLNAPlay.RS_OK;
		
	}

	/**
	 * stop play dlna video
	 * @param uuid device server_uid
	 * @return int result
	 * @throws
	 */
	public static int stop(final String uuid) {
		new Thread(){
			public void run(){
		try {
			L.i("airone","java stop");
			AirOne.getInstance().getService().dlna_stop(uuid);
		} catch (RemoteException e) {
			e.printStackTrace();
			L.i("airone","airone_dlna_observer_get_play_state error:"+e.toString());
		}
			}
		}.start();
		return  FDLNAPlay.RS_OK;
	}

	/**
	 * set_volume to device 
	 * @param uuid device server_uid
	 * @param volume volume value
	 * @return int result
	 * @throws
	 */
	public static int set_volume(final String uuid,final int volume) {
		
		new Thread(){
			public void run(){
				
				try {
					AirOne.getInstance().getService().dlna_set_volume(uuid,volume);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		}.start();
		return  FDLNAPlay.RS_OK;
		
	}
	static int NONE = -100;
	static int last_state = NONE;
	/**
	 * get dlna play state
	 * @param uuid device server_uid
	 * @return int result
	 * @throws
	 */
	public static int get_play_state(String uuid) {
		try {
			int ret =  AirOne.getInstance().getService().dlna_get_play_state(uuid);
			L.i("airone","get_play_state:"+ret);
			if( ret == CDLNAPlayer.PLAYSTATE_STOPPED && last_state == NONE )
				return CDLNAPlayer.PLAYSTATE_PLAYING;
			else
				last_state = ret;
			return ret;
			
		} catch (RemoteException e) {
			L.i("airone","airone_dlna_observer_get_play_state error:"+e.toString());
			e.printStackTrace();
		}
		return  FDLNAPlay.RS_ERROR;
	}

	/**
	 * 
	 * get dlna device play volume
	 * @param uuid device server_uid
	 * @return int result
	 * @throws
	 */
	public static int get_play_volume(String uuid) {
		try {
			return AirOne.getInstance().getService().dlna_get_play_volume(uuid);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return  FDLNAPlay.RS_ERROR;
	}

	/**
	 * get dlna device video duration
	 * @param uuid device server_uid
	 * @return int result
	 * @throws
	 */
	public static int get_duration(String uuid) {
		try {
			return AirOne.getInstance().getService().dlna_get_duration(uuid);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return  FDLNAPlay.RS_ERROR;
	}
	
	/**
	 * get dlna device play progress 
	 * @param uuid device server_uid
	 * @return int result
	 * @throws
	 */
	public static int get_play_curtime(String uuid) {
		
		try {
			return AirOne.getInstance().getService().dlna_get_play_curtime(uuid);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return  FDLNAPlay.RS_ERROR;
	}

	

}
